package com.benhil.hrms.util;

import java.util.List;

public class JsonResponseContainer {
	
	private static JsonResponseContainer _instance = new JsonResponseContainer(); 
	
	public static Response createErrorResponse(String message){
		return _instance.createSuccessMessage(false, message);
	}
	
	public static Response createSuccessResponse(String message){
		return _instance.createSuccessMessage(true, message);
	}
	
	public static <E>Response createSuccessResponse(List<E> data){
		return _instance.createSuccessModel(data);
	}

	public class Response {
		
		protected boolean success;
		
		public Response(boolean isSuccess) {
			success = isSuccess;
		}

		public boolean isSuccess() {
			return success;
		}

		public void setSuccess(boolean success) {
			this.success = success;
		}		
	}
	
	public class ResponseModel<T> extends Response {
		
		public ResponseModel(List<T> list) {
			super(true);
			data = list;
			total = data == null ? 0 : data.size();
		}
		
		private int total;
		private List<T> data;
		
		public int getTotal() {
			return total;
		}
		public void setTotal(int total) {
			this.total = total;
		}
		public List<T> getData() {
			return data;
		}
		public void setData(List<T> data) {
			this.data = data;
		}		
	}
	
	private <T>ResponseModel<T> createSuccessModel(List<T> list){
		return new ResponseModel<T>(list);
	}
	
	public class ResponseMessage extends Response {
		
		public ResponseMessage(boolean success, String msg) {
			super(success);
			message = msg;
		}
		
		private String message;
		
		public String getMessage() {
			return message;
		}
		public void setMessage(String message) {
			this.message = message;
		}			
	}
	
	private ResponseMessage createSuccessMessage(boolean success, String message){
		return new ResponseMessage(success, message);
	}
}
